/**
 * Created by cxj on 2016/2/22.
 */



var grid;

var bootbox_global;
//删除对象
function deleteObj(objid)
{


    // alert(roleid);

    var bootbox= bootbox_global;

    bootbox_global.dialog({
        message: "确定要删除该用户吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/adminUser/deleteadminuser", "user_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}


//修改对象
function updateObj(objid,datarow)
{

    bootbox= bootbox_global;

    var obj = grid.date_list[datarow];

    //alert(roleid);


    var alertarray = {
        form: {api: "/adminUser/changeuserinfo", name: "alertform", id: "alertform"},
        inputs: [
            {title: "用户名", type: "text", placeholder: "", verify: "empty", name: "user_name", id: "user_name",value:obj.user_name,readonly:"readonly"},

            {title: "姓名", type: "text", placeholder: "", verify: "empty", name: "real_name", id: "real_name",value:obj.real_name},

            {title: "角色", type: "select", id: "roles",form_name:"role_id",copyid:"roleselect",selectvalue:obj.role_id},
        ]

    }
    bootbox.dialog({
        title: "修改用户",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&user_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });

}


//重置对象
function resetObj(objid)
{


    // alert(roleid);

    var bootbox= bootbox_global;

    bootbox_global.dialog({
        message: "确定要重置该用户密码吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/adminUser/resetpassword", "user_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}

define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {

        bootbox_global = bootbox;


        var columns_proj ={name_dataIndex :'role_name',value_dataIndex :'role_id'};

        var  grid_citylist = new Select_Grid("/role/getrolelist",$("#roleselect"),columns_proj,"");

        grid_citylist.Init(function(){



        });


        //每列数据列性
        var columns = [
            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
            }},
            { title: '用户名', dataIndex: 'user_name'},
            { title: '姓名', dataIndex: 'real_name'},
            { title: '角色', dataIndex: 'role_name'},

            { title: '创建时间', dataIndex: 'create_time',},
            { title: '修改时间', dataIndex: 'update_time',},
            {title : '操作',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var  html =  "<div class='div_btn'><button onclick=\"updateObj("+data[datarow].user_id+","+datarow+")\" class='btn btn-info'>修改</button> <button onclick=\"deleteObj("+data[datarow].user_id+")\"  class='btn btn-danger'>删除</button>";


                //如果是一级菜单


                    html = html+" <button onclick=\"resetObj("+data[datarow].user_id+")\" class='btn btn-success'>重置密码</button>";


                html = html+"</div>"
                return html;
            }},


        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/adminUser/getadminuserlist", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param="user_name="+$("#user_name").val();

            return param;
        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });

        //新增用户
        $("#btn_add").bind("click",function() {


            var alertarray = {
                form: {api: "/adminUser/addadminuser", name: "alertform", id: "alertform"},
                inputs: [

                    {title: "用户名", type: "text", placeholder: "", verify: "empty", name: "user_name", id: "user_name"},
                    {title: "姓名", type: "text", placeholder: "", verify: "empty", name: "real_name", id: "real_name"},
                    {title: "角色", type: "select", id: "roles",form_name:"role_id",copyid:"roleselect"},

                ]

            }
            bootbox.dialog({
                title: "新增用户",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });
    });


});